package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;



    /**
     * 加载首页-加载更多-加载更新 三位一体
     *
     * @param dto  type=1 认为是加载更多，type=2表示加载更新
     * @param type
     * @return
     */
    public ResponseResult load(ArticleHomeDto dto, Short type) {

        List<ApArticle> apArticleList = this.getBaseMapper().loadArticleList(dto, type);

        return ResponseResult.okResult(apArticleList);
    }


    /**
     * 保存三剑客
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult save(ArticleDto dto) {
       //TODO
        //1.是否存在文章id
        if (dto.getId()==null){
            //2.不存在保存文章
            ApArticle apArticle = BeanUtil.copyProperties(dto, ApArticle.class);
            articleMapper.insert(apArticle);//插入文章
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsDown(false);
            articleConfigMapper.insert(apArticleConfig);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.insert(apArticleContent);
            dto.setId(apArticle.getId());
            return ResponseResult.okResult(apArticle.getId());
        }else {
            //2.2存在修改文章
            ApArticle apArticle = BeanUtil.copyProperties(dto, ApArticle.class);
            LambdaUpdateWrapper<ApArticle> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(ApArticle::getId,dto.getId());
            articleMapper.update(apArticle,updateWrapper);
            return ResponseResult.okResult(dto.getId());
        }
    }

}
